<template>
  <XtxDialog :title="(formData.id?'编辑':'添加')+'收货地址'" v-model="dialogVisible">
    <div class="address-edit">
      <Form ref="formCom" class="xtx-form" :validation-schema="veeValidateSchema" v-slot="{ errors }">
        <!-- {{errors}} -->
      <div class="xtx-form-item">
        <div class="label">收货人：</div>
        <div class="field">
          <Field type="text" name='receiver'  v-model="formData.receiver" class="input" placeholder="请输入收货人" />
        </div>
        <div v-if="errors.receiver" class="error">
            <i class="iconfont icon-warning" />{{ errors.receiver }}
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">手机号：</div>
        <div class="field">
          <input  v-model="formData.contact" class="input" placeholder="请输入手机号" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地区：</div>
        <div class="field">
          <XtxCity :fullLocation="formData.fullLocation" @change="changeCty" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">详细地址：</div>
        <div class="field">
          <input  v-model="formData.address" class="input" placeholder="请输入详细地址" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">邮政编码：</div>
        <div class="field">
          <input v-model="formData.postalCode" class="input" placeholder="请输入邮政编码" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地址标签：</div>
        <div class="field">
          <input v-model="formData.addressTags" class="input" placeholder="请输入地址标签，逗号分隔" />
        </div>
      </div>
    </Form>
    </div>
    <template v-slot:footer>
      <XtxButton @click="dialogVisible = false" type="gray" style="margin-right:20px">取消</XtxButton>
      <XtxButton @click="submit" type="primary">确认</XtxButton>
    </template>
  </XtxDialog>
</template>
<script>
import { Field, Form } from 'vee-validate'
import veeValidateSchema from '@/utils/vee-validate-schema'
import Message from '@/components/library/Message'
import { addAddress, editAddress } from '@/api/order'
import { reactive, ref } from 'vue'
export default {
  name: 'AddressEdit',
  emits: ['success'],
  components: {
    Field, Form
  },
  setup (props, { emit }) {
    const formCom = ref(null)

    const dialogVisible = ref(false)
    // 打开函数
    const open = () => {
      dialogVisible.value = true
      formCom.value.resetForm()
    }
    // 表单数据
    const formData = reactive({
      receiver: '', // 收货人姓名
      contact: '', // 联系方式
      provinceCode: '', // 所在省份编码
      cityCode: '', // 所在城市编码
      countyCode: '', // 所在区/县编码
      address: '', // 详细地址
      postalCode: '', // 邮政编码
      addressTags: '', // 地址标签,以英文逗号分割
      isDefault: 0, // 是否为默认，0为是，1为否
      fullLocation: '', // 选完地址后的全部地址
      id: ''
    })

    // 选择地区
    const changeCty = (data) => {
      formData.provinceCode = data.provinceCode
      formData.cityCode = data.cityCode
      formData.countyCode = data.countyCode
      formData.fullLocation = data.fullLocation
    }
    // 提交操作
    const submit = async () => {
      if (formData.id) {
        await editAddress(formData)
        Message({ text: '修改收货地址成功', type: 'success' })
        dialogVisible.value = false
        emit('success', formData)
      } else {
        const { result } = await addAddress(formData)
        // 添加成功
        Message({ text: '添加收货地址成功', type: 'success' })
        formData.id = result.id
        dialogVisible.value = false
        emit('success', formData)
      }
    }
    return { dialogVisible, open, formData, changeCty, submit, veeValidateSchema, formCom }
  }
}
</script>
<style scoped lang="less">
.address-edit {
.xtx-dialog {
  :deep(.wrapper){
    width: 780px;
    .body {
      font-size: 14px;
    }
  }
}
.xtx-form {
  padding: 0;
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
}
.xtx-city {
  width: 320px;
  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}
}
</style>
